package com.xsomnus.network_programming.netty.white;

import com.xsomnus.network_programming.lib.User;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/**
 * @author @xsomnus666_xiawenye★
 * @since 2019/7/4 0004 15:13
 * - 才需学也,学需静也/非淡泊无以明志，非宁静无以致远
 */
public class ClientHandler1 extends ChannelInboundHandlerAdapter {


    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        User.HelloRequest helloRequest = User.HelloRequest.newBuilder()
                .setUsername("hello,protobuf")
                .build();
        final byte[] bytes = helloRequest.toByteArray();
        final ByteBufAllocator bufAllocator = ctx.alloc();
        final ByteBuf byteBuf = bufAllocator.buffer(bytes.length);
        byteBuf.writeBytes(bytes);
        ctx.writeAndFlush(byteBuf);

    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        super.channelRead(ctx, msg);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        super.exceptionCaught(ctx, cause);
    }
}
